package dbExec

//import (
//	"fmt"
//	"log"
//	//"report/src/config"
//	"time"
//
//	"gorm.io/driver/mysql"
//	"gorm.io/driver/sqlite"
//	"gorm.io/gorm"
//)
//
//// db连接
//var db *gorm.DB
//
//// Setup 初始化连接
//func (dbexec DBConnStruct) Setup() {
//	// db = newConnection()
//	var dbURI string
//	var dialector gorm.Dialector
//	if dbexec.DBDevice == "mysql" {
//		dbURI = fmt.Sprintf("%s", dbexec.JDBC)
//		dialector = mysql.New(mysql.Config{
//			DSN:                       dbURI, // data source name
//			DefaultStringSize:         256,   // default size for string fields
//			DisableDatetimePrecision:  true,  // disable datetime precision, which not supported before MySQL 5.6
//			DontSupportRenameIndex:    true,  // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
//			DontSupportRenameColumn:   true,  // `change` when rename column, rename column not supported before MySQL 8, MariaDB
//			SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
//		})
//	} else if dbexec.DBDevice == "postgres" {
//		//dbURI = fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s",
//		//	config.DatabaseSetting.Host,
//		//	config.DatabaseSetting.Port,
//		//	config.DatabaseSetting.User,
//		//	config.DatabaseSetting.Name,
//		//	config.DatabaseSetting.Password)
//		//dialector = postgres.New(postgres.Config{
//		//	DSN:                  "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai",
//		//	PreferSimpleProtocol: true, // disables implicit prepared statement usage
//		//})
//	} else if dbexec.DBDevice == "oracle" {
//		dbURI = fmt.Sprintf("%s", dbexec.JDBC)
//		dialector = mysql.New(mysql.Config{
//			DSN:                       dbURI, // data source name
//			DefaultStringSize:         256,   // default size for string fields
//			DisableDatetimePrecision:  true,  // disable datetime precision, which not supported before MySQL 5.6
//			DontSupportRenameIndex:    true,  // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
//			DontSupportRenameColumn:   true,  // `change` when rename column, rename column not supported before MySQL 8, MariaDB
//			SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
//		})
//	} else { // sqlite3
//		dbURI = fmt.Sprintf("test.db")
//		dialector = sqlite.Open("test.db")
//	}
//	conn, err := gorm.Open(dialector, &gorm.Config{})
//	if err != nil {
//		log.Print(err.Error())
//	}
//	sqlDB, err := conn.DB()
//	if err != nil {
//		fmt.Println("connect db server failed.")
//	}
//	sqlDB.SetMaxIdleConns(10)                   // SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
//	sqlDB.SetMaxOpenConns(100)                  // SetMaxOpenConns sets the maximum number of open connections to the database.
//	sqlDB.SetConnMaxLifetime(time.Second * 600) // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
//	db = conn
//}
//
//// GetDB 开放给外部获得db连接
//func (dbexec DBConnStruct) GetDB() *gorm.DB {
//	sqlDB, err := db.DB()
//	if err != nil {
//		fmt.Errorf("connect db server failed.")
//		dbexec.Setup()
//	}
//	if err := sqlDB.Ping(); err != nil {
//		sqlDB.Close()
//		dbexec.Setup()
//	}
//	return db
//}
